package DataBase;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Vector;

import DataObjects.User;

public class AddEmployee {
	
	private User employee;
	private int status=0;  
	
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	
	//
	public AddEmployee(User emp)
	{
		this.employee=emp;
	}
	
	public void saveNewEmp()
	{
	
			SQLConnection sc=new SQLConnection();
			
			try {
									        
			
			PreparedStatement ps3=sc.getConnection().prepareStatement("INSERT INTO Employee (nameBank,name,phoneNo,role,hireDate,email,house,city,eid,street,lastName,numBranch,numAccount,birthday,idNumber,permission,status) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);");
			
			sc.getConnection().setAutoCommit(false);
			
			
			
			
			if(employee.getPermission()!=0)
			{
			PreparedStatement ps1 = sc.getConnection().prepareStatement("INSERT INTO Login (username,password) values(?,?);");
			ps1.setString(1, employee.getUsername());
			ps1.setString(2, employee.getpassword());
			ps1.executeUpdate();
			sc.getConnection().commit();
			ps1.close();
			}
			
			
			
			ps3.setString(1, employee.getNameBank());
			ps3.setString(2, employee.getName());
			ps3.setString(3, employee.getPhoneNo());
			ps3.setString(4, employee.getRole());
			ps3.setString(5, employee.getHireDate());
			ps3.setString(6, employee.getEmail());
			ps3.setString(7, employee.getHouse());
			ps3.setString(8, employee.getCity());
			ps3.setInt(9, employee.getEid());
			ps3.setString(10, employee.getStreet());
			ps3.setString(11, employee.getLastName());
			ps3.setString(12, employee.getNumBranch());
			ps3.setString(13, employee.getNumAccount());
			ps3.setString(14, employee.getBirthday());
			ps3.setString(15, employee.getIdNumber());
			ps3.setInt(16, employee.getPermission());
			ps3.setString(17, employee.getStatus());
			ps3.executeUpdate();
			sc.getConnection().commit();
			
			if(employee.getPermission()!=0)
			{
			PreparedStatement ps2=sc.getConnection().prepareStatement("INSERT INTO Employee_Login (username,eid,status) values(?,?,0);");
			ps2.setString(1, employee.getUsername());
			ps2.setInt(2, employee.getEid());
			ps2.executeUpdate();
			
			ps2.close();
			}
			
			if(employee.getWeekHours()!=null)
			{
				Vector<Vector> weekHours=employee.getWeekHours();
				PreparedStatement ps4 = sc.getConnection().prepareStatement("INSERT INTO EmployeeDaysOfWeek (eid,day,hour) values(?,?,?);");
				for(int i=0;i<weekHours.size();i++)
				{
					Vector<String> dayHours=weekHours.get(i);
					String day=(String)dayHours.get(0);
					int from=Integer.parseInt((String)dayHours.get(1));
					int to=Integer.parseInt((String)dayHours.get(2));
					for(int j=from;j<=to;j++)
					{
						ps4.setInt(1, employee.getEid());
						ps4.setString(2, day);
						ps4.setString(3, Integer.toString(j));
						ps4.executeUpdate();
					}
				}
				ps4.close();
			}
			sc.getConnection().commit();
			ps3.close();
            this.status=1;
            sc.getConnection().close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

}
